package com.snowleopard.findpeople.web.publish;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springside.modules.orm.Page;

import com.snowleopard.findpeople.entity.publish.InfoPublish;
import com.snowleopard.findpeople.service.mgmt.ParameterManager;
import com.snowleopard.findpeople.service.publish.InfoPublishManager;
import com.snowleopard.findpeople.web.CrudActionSupport;

@Namespace("/publish")
public class PublishInfoAction extends CrudActionSupport<InfoPublish> {
    private static final Logger log = LoggerFactory.getLogger(PublishInfoAction.class);

    private static final long serialVersionUID = -3570522433349351049L;

    @Autowired
    private InfoPublishManager InfoPublishManager;
    @Autowired
    private ParameterManager parameterManager;

    private String infoPublishId;
    private InfoPublish infoPublish;

    @Value("#{fpProperties['publish.info.page.size']}")
    private String PAGE_SIZE;

    private Page<InfoPublish> page;

    private boolean clean;

    @Override
    public String delete() throws Exception {

        log.debug("infoPublishId={}", infoPublishId);
        InfoPublishManager.deleteInfoPublish(infoPublishId);

        addActionMessage("删除发布信息成功...");
        return SUCCESS;
    }

    public InfoPublish getInfoPublish() {
        return infoPublish;
    }

    public String getInfoPublishId() {
        return infoPublishId;
    }

    @Override
    public InfoPublish getModel() {
        return infoPublish;
    }

    public Page<InfoPublish> getPage() {
        return page;
    }

    public ParameterManager getParameterManager() {
        return parameterManager;
    }

    @Override
    public String input() throws Exception {
        if (StringUtils.isNotBlank(infoPublishId)) {
            infoPublish = InfoPublishManager.getInfoPublish(infoPublishId);
        }
        return INPUT;
    }

    public void readImg() {

    }

    public boolean isClean() {
        return clean;
    }

    @Override
    public String list() throws Exception {
        if (clean) {
            infoPublish = null;
            return SUCCESS;
        }

        if (infoPublish == null) {
            return SUCCESS;
        }

        if (page == null) {
            page = new Page<InfoPublish>(Integer.valueOf(PAGE_SIZE));
        } else {
            page.setPageSize(Integer.valueOf(PAGE_SIZE));
        }

        if (!page.isOrderBySetted()) {
            page.setOrderBy("id");
            page.setOrder(Page.ASC);
        }

        page = InfoPublishManager.searchPublishInfoByCondition(page, infoPublish);

        return SUCCESS;
    }

    @Override
    public String save() throws Exception {

        InfoPublishManager.saveInfoPublish(infoPublish);

        addActionMessage("发布信息保存成功.");
        return INPUT;
    }

    public void setClean(final boolean clean) {
        this.clean = clean;
    }

    public void setInfoPublish(final InfoPublish infoPublish) {
        this.infoPublish = infoPublish;
    }

    public void setInfoPublishId(final String infoPublishId) {
        this.infoPublishId = infoPublishId;
    }

    public void setPage(final Page<InfoPublish> page) {
        this.page = page;
    }

}
